import { Autowired } from '@opensumi/di';
import { Domain, localize, CommandContribution, CommandRegistry, URI, ClientAppContribution, getIcon, MaybeNull } from '@opensumi/ide-core-browser';
import { LowcodeContainerId, LowcodeComponentId } from '../common';
import { EditorView } from './editor.view';
import WelcomeView from './welcome.view'
import FastView from './fast.view';
import { LOWCODE_COMMANDS, ILowcodeService, LowcodePageScheme } from '../common';
import { ResourceService, IResource, WorkbenchEditorService, IResourceProvider } from '@opensumi/ide-editor';
import { ComponentContribution, ComponentRegistry } from '@opensumi/ide-core-browser/lib/layout';

import {
  BrowserEditorContribution,
  EditorComponentRegistry,
  EditorComponentRenderMode,
} from '@opensumi/ide-editor/lib/browser';

@Domain(ComponentContribution, CommandContribution, BrowserEditorContribution, ClientAppContribution)
export class LowcodeContribution implements ComponentContribution, CommandContribution, BrowserEditorContribution, ClientAppContribution {

  @Autowired(ILowcodeService)
  private lowcodeService: ILowcodeService;

  @Autowired(WorkbenchEditorService)
  private readonly editorService: WorkbenchEditorService;

  @Autowired()
  editorComponentRegistry: EditorComponentRegistry;

  // 注册viewContainer 和 view
  registerComponent(registry: ComponentRegistry) {
    registry.register('ide-lowcode', [{
      component: WelcomeView,
      collapsed: false,
      id: 'lowcode-welcome',
      name: 'WELCOME',
    },{
      component: FastView,
      collapsed: true,
      id: 'lowcode-fast',
      name: '快速入口',
    },], {
      iconClass: getIcon('code'),
      title: '低代码',
      priority: 10,
      containerId: LowcodeContainerId,
    });
  }

  
  // 注册命令
  registerCommands(commands: CommandRegistry) {
    // 添加命令（command + shift + p）搜索lowcode
    commands.registerCommand(LOWCODE_COMMANDS.CREATE_PROJECT, {
      execute: () => {
        return this.lowcodeService.showMessage('创建项目');
      },
    });

    commands.registerCommand(LOWCODE_COMMANDS.CREATE_PAGE, {
      execute: () => {
        return this.lowcodeService.showMessage('创建页面');
      },
    });
  }

  

  // 注册uri的scheme
  registerResource(service: ResourceService) {
    console.log('registerResourceProvider');
    service.registerResourceProvider({
      scheme: LowcodePageScheme,
      provideResource: async (uri: URI): Promise<IResource<{}>> => {
        // 每次open触发
        const query = uri.getParsedQuery();
        return Promise.resolve({
          supportsRevive: false,
          uri,
          name: query.name,
          icon: getIcon('code'),
          metadata: {
            query,
          },
        })
      },
      shouldCloseResource: (resource: IResource<any>, openedResources: IResource<any>[][]) {
        for (const resources of openedResources) {
          for (const r of resources) {
            console.log(r.uri, r.uri.toString(), resource.uri.toString());
            if (r.uri.scheme === LowcodePageScheme && r.uri.toString() === resource.uri.toString()) {
              // 清楚页面缓存
              this.editorComponentRegistry.clearPerWorkbenchComponentCache(LowcodeComponentId);
            }
          }
        }
        return true;
      },
      onDisposeResource: (resource: IResource<any>) => {
        // 关闭时触发
        console.log('onDisposeResource,',resource)
      }
    });
  }

  // 注册编辑器组件
  registerEditorComponent(registry: EditorComponentRegistry) {
    registry.registerEditorComponent({
      uid: LowcodeComponentId,
      scheme: LowcodePageScheme,
      component: EditorView,
      renderMode: EditorComponentRenderMode.ONE_PER_WORKBENCH,
    });
    registry.registerEditorComponentResolver(LowcodePageScheme, (resource, results) => {
      results.push({
        type: 'component',
        componentId: LowcodeComponentId,
      });
    });
  }

  onDidRender() {
    console.log('onDidRender')
  }

  onDidStart() {
    console.log('onDidStart')
  }
}
